package org.sws.domain.activity.service.partake;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.sws.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import org.sws.domain.activity.model.entity.*;
import org.sws.domain.activity.model.valobj.UserRaffleOrderStateVO;
import org.sws.domain.activity.repository.IActivityRepository;
import org.sws.types.enums.ResponseCode;
import org.sws.types.exception.AppException;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author sws
 * @Date 2025/3/21 09:02
 * @description: 参与抽奖服务实现类
 */
@Slf4j
@Service
public class ActivityPartakeServiceService extends AbstractActivityPartakeServiceService {
    public ActivityPartakeServiceService(IActivityRepository activityRepository) {
        super(activityRepository);
    }

    private final SimpleDateFormat dateFormatMonth = new SimpleDateFormat("yyyy-MM");
    private final SimpleDateFormat dateFormatDay = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected CreatePartakeOrderAggregate doFilterAndBuildAccount(String userId, Long activityId, Date currentTime) {
        // 查询总账户额度
        ActivityAccountEntity activityAccountEntity = activityRepository.queryActivityAccountByUserId(userId, activityId);
        // 额度判断（只判断总剩余额度）
        if (null == activityAccountEntity || activityAccountEntity.getTotalCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_QUOTA_ERROR.getInfo());
        }
        String month = dateFormatMonth.format(currentTime);
        String day = dateFormatDay.format(currentTime);
        // 查询当前月的月额度账户
        ActivityAccountMonthEntity activityAccountMonthEntity = activityRepository.queryactivityMonthAccount(userId, activityId, month);
        if (null != activityAccountMonthEntity && activityAccountMonthEntity.getMonthCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_MONTH_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_MONTH_QUOTA_ERROR.getInfo());
        }
        boolean isExistAccountMonth = null != activityAccountMonthEntity;
        if (!isExistAccountMonth) {
            activityAccountMonthEntity = ActivityAccountMonthEntity.builder()
                    .userId(userId)
                    .activityId(activityId)
                    .month(month)
                    .monthCount(activityAccountEntity.getMonthCount())
                    .monthCountSurplus(activityAccountEntity.getMonthCountSurplus())
                    .build();
        }
        // 查询当日的日额度账户
        ActivityAccountDayEntity activityAccountDayEntity = activityRepository.queryactivityDayAccount(userId, activityId, day);
        if (null != activityAccountDayEntity && activityAccountDayEntity.getDayCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_DAY_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_DAY_QUOTA_ERROR.getInfo());
        }
        boolean isExistAccountDay = null != activityAccountDayEntity;
        if (!isExistAccountDay) {
            activityAccountDayEntity = ActivityAccountDayEntity.builder()
                    .userId(userId)
                    .activityId(activityId)
                    .day(day)
                    .dayCount(activityAccountEntity.getDayCount())
                    .dayCountSurplus(activityAccountEntity.getDayCountSurplus())
                    .build();
        }

        return CreatePartakeOrderAggregate.builder()
                .userId(userId)
                .activityId(activityId)
                .activityAccountEntity(activityAccountEntity)
                .isExistAccountMonth(isExistAccountMonth)
                .activityAccountMonthEntity(activityAccountMonthEntity)
                .isExistAccountDay(isExistAccountDay)
                .activityAccountDayEntity(activityAccountDayEntity)
                .build();
    }

    @Override
    protected UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentTime) {
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);
        return UserRaffleOrderEntity.builder()
                .userId(userId)
                .strategyId(activityEntity.getStrategyId())
                .activityId(activityId)
                .activityName(activityEntity.getActivityName())
                .orderId(RandomStringUtils.randomNumeric(12))
                .orderState(UserRaffleOrderStateVO.create)
                .orderTime(currentTime)
                .activityEndTime(activityEntity.getEndDateTime())
                .build();
    }

    @Override
    public ActivityAccountEntity queryAccountSurplus(String userId, Long activityId) {
        return activityRepository.queryAccountSurplus(userId, activityId);
    }
}
